﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class PositionDao:IPositionDao
    {
        private EFCoreDbContext _context;
        public PositionDao(EFCoreDbContext context)
        {
            _context = context;
        }

        public void Add(Position position)
        {
            _context.Positions.Add(position);
            _context.SaveChanges();
        }

        public void Delete(int id)
        {
            var position = _context.Positions.FirstOrDefault(e => e.Id == id);
            _context.Positions.Remove(position);
            _context.SaveChanges();
        }

        public Position GetById(int id)
        {
            return _context.Positions.FirstOrDefault(e => e.Id == id);
        }

        public int GetPositionCount(string sql)
        {
            return _context.Positions.FromSql(sql).Count();
        }

        public IList<Position> GetPositionList(int companyId, int status)
        {
            return _context.Positions.Where(e => e.CompanyId == companyId && e.Status == status).ToList();
        }

        public IList<Position> GetPositionList(string sql)
        {
            return _context.Positions.FromSql(sql).OrderByDescending(e => e.CreateTime).ToList();
        }

        public void Update(Position position)
        {
            _context.Positions.Update(position);
            _context.SaveChanges();
        }
    }
}
